<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-01-01
 * Time: 23:09
 */

namespace extend\oss;


class Base
{
    protected $config = [];

    public function __construct()
    {
        $this->config = [
            'accessKeyId' => config('ali.accessKeyId'),
            'accessKeySecret' => config('ali.accessKeySecret'),
            'endpoint' => config('ali.endpoint'),
            'bucket' => config('ali.bucket'),
            'url' => config('ali.url'),
        ];
    }


    /**
     * 生成文件名
     * @param $file
     * @param array $option
     * @return string
     */
    protected function buildName($file, $option = [])
    {
        $name = '';
        if (!empty($option['replace'])) {
            $name = pathinfo($file->getInfo('name'), PATHINFO_BASENAME);
        } else {
            $name = date('Ymd') . DIRECTORY_SEPARATOR . md5(uniqid());
        }

        if (false === strpos($name, '.')) {
            $name .= '.' . pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        }

        return $name;
    }

    /**
     * 获取iso8601型日期
     * @param $time
     * @return string
     * @throws \Exception
     */
    protected function gmtIso8601($time)
    {
        $dtStr = date("c", $time);
        $dataTime = new \DateTime($dtStr);
        $expiration = $dataTime->format(\DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        $expiration = substr($expiration, 0, $pos);
        return $expiration . "Z";
    }
}